package com.furongji.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.furongji.common.domain.vo.CourseViewCountVo;
import com.furongji.education.clients.CourseFeignClient;
import com.furongji.education.clients.OrderFeignClient;
import com.furongji.education.clients.UserFeignClient;
import com.furongji.education.domain.po.DailyStatistics;
import com.furongji.education.domain.vo.DailyStatisticsVo;
import com.furongji.education.mapper.DailyStatisticsMapper;
import com.furongji.education.service.DailyStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author furongji
 * @since 2025-06-01
 */
@Service
@RequiredArgsConstructor
public class DailyStatisticsServiceImpl extends ServiceImpl<DailyStatisticsMapper, DailyStatistics> implements DailyStatisticsService {

    private final UserFeignClient userClient;

    private final CourseFeignClient courseClient;

    private final OrderFeignClient orderClient;

    @Value("${statistics.debug}")
    private boolean debug;

    @Override
    public DailyStatisticsVo getSummaryStatistics(String date) {

        LocalDate statDate = LocalDate.parse(date);
        Integer registerCount = userClient.registerCount(date);
        Integer loginCount = userClient.loginCount(date);
        Integer addCourseCount = courseClient.courseInsertCount(date);
        List<CourseViewCountVo> courseViewCountVos = courseClient.courseViewCount(date);
        Integer articleCount = 50;
        Integer commentCount = 100;
        Integer orderCount = orderClient.orderCount(date);

        DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo(
                statDate,
                registerCount,
                loginCount,
                addCourseCount,
                courseViewCountVos,
                articleCount,
                commentCount,
                orderCount
        );


        // 假数据，可以删除
        if(debug) {
            dailyStatisticsVo = generateDate(statDate);
            registerCount = dailyStatisticsVo.getRegisterCount();
            loginCount = dailyStatisticsVo.getLoginCount();
            addCourseCount = dailyStatisticsVo.getCourseAddCount();
            articleCount = dailyStatisticsVo.getArticleCount();
            commentCount = dailyStatisticsVo.getCommentCount();
            orderCount = dailyStatisticsVo.getOrderCount();
            courseViewCountVos = dailyStatisticsVo.getCourseViewCountVoList();
        }

        DailyStatistics isExist = super.getOne(
                new LambdaQueryWrapper<DailyStatistics>()
                        .eq(DailyStatistics::getStatisticalDate, statDate));

        if (isExist != null) {
            fillStatistics(isExist, statDate, registerCount, loginCount, addCourseCount,
                    articleCount, commentCount, orderCount, courseViewCountVos);
            super.updateById(isExist);
        } else {
            DailyStatistics dailyStatistics = new DailyStatistics();
            fillStatistics(dailyStatistics, statDate, registerCount, loginCount, addCourseCount,
                    articleCount, commentCount, orderCount, courseViewCountVos);
            super.save(dailyStatistics);
        }

        return dailyStatisticsVo;
    }

    private void fillStatistics(DailyStatistics ds,
                                LocalDate statDate,
                                Integer registerCount,
                                Integer loginCount,
                                Integer addCourseCount,
                                Integer articleCount,
                                Integer commentCount,
                                Integer orderCount,
                                List<CourseViewCountVo> courseViewCountVos) {

        ds.setStatisticalDate(statDate)
                .setRegisterCount(registerCount)
                .setLoginCount(loginCount)
                .setCourseAddCount(addCourseCount)
                .setArticleCount(articleCount)
                .setCommentCount(commentCount)
                .setOrderCount(orderCount)
                .setCourseViewCount(courseViewCountVos.stream()
                        .mapToInt(CourseViewCountVo::getCourseViewCount).sum());
    }


    // 生成假数据
    private DailyStatisticsVo generateDate(LocalDate date) {
        Integer registerCount = 129;
        Integer loginCount = 500;
        Integer addCourseCount = 500;
        Integer articleCount = 5000;
        Integer commentCount = 600;
        Integer orderCount = 330;

        // 构造假课程浏览量列表
        List<CourseViewCountVo> courseViewCountVos = new ArrayList<>();
        courseViewCountVos.add(new CourseViewCountVo("1", "Vue入门", 123));
        courseViewCountVos.add(new CourseViewCountVo("2", "Spring Boot实战", 98));
        courseViewCountVos.add(new CourseViewCountVo("3", "前端性能优化", 75));
        courseViewCountVos.add(new CourseViewCountVo("4", "算法基础", 61));
        courseViewCountVos.add(new CourseViewCountVo("5", "网络安全入门", 43));
        courseViewCountVos.add(new CourseViewCountVo("6", "Java基础", 110));
        courseViewCountVos.add(new CourseViewCountVo("7", "Python数据分析", 95));
        courseViewCountVos.add(new CourseViewCountVo("8", "机器学习入门", 85));
        courseViewCountVos.add(new CourseViewCountVo("9", "数据库设计", 70));
        courseViewCountVos.add(new CourseViewCountVo("10", "前端框架实战", 65));
        courseViewCountVos.add(new CourseViewCountVo("11", "微服务架构", 60));
        courseViewCountVos.add(new CourseViewCountVo("12", "Linux运维", 55));
        courseViewCountVos.add(new CourseViewCountVo("13", "移动开发", 50));
        courseViewCountVos.add(new CourseViewCountVo("14", "人工智能基础", 48));
        courseViewCountVos.add(new CourseViewCountVo("15", "软件测试", 45));
        courseViewCountVos.add(new CourseViewCountVo("16", "网络编程", 42));
        courseViewCountVos.add(new CourseViewCountVo("17", "数据结构", 40));
        courseViewCountVos.add(new CourseViewCountVo("18", "大数据技术", 38));
        courseViewCountVos.add(new CourseViewCountVo("19", "区块链入门", 35));
        courseViewCountVos.add(new CourseViewCountVo("20", "云计算", 33));
        courseViewCountVos.add(new CourseViewCountVo("21", "DevOps实践", 30));
        courseViewCountVos.add(new CourseViewCountVo("22", "前端工程化", 28));
        courseViewCountVos.add(new CourseViewCountVo("23", "图像处理", 26));
        courseViewCountVos.add(new CourseViewCountVo("24", "深度学习", 25));
        courseViewCountVos.add(new CourseViewCountVo("25", "游戏开发", 23));
        courseViewCountVos.add(new CourseViewCountVo("26", "系统设计", 22));
        courseViewCountVos.add(new CourseViewCountVo("27", "敏捷开发", 20));
        courseViewCountVos.add(new CourseViewCountVo("28", "软件架构", 19));
        courseViewCountVos.add(new CourseViewCountVo("29", "数据挖掘", 18));
        courseViewCountVos.add(new CourseViewCountVo("30", "UI设计", 15));

        DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo(
                date,
                registerCount,
                loginCount,
                addCourseCount,
                courseViewCountVos,
                articleCount,
                commentCount,
                orderCount
        );
        return dailyStatisticsVo;
    }
}
